FROM openjdk:17-jdk-slim

LABEL maintainer="your-email@example.com"
LABEL description="Apache Flume with Hadoop and Snappy support"

# 设置环境变量
ENV FLUME_VERSION=1.11.0
ENV HADOOP_VERSION=3.4.0
ENV JINDOSDK_VERSION=6.1.6
ENV FLUME_HOME=/opt/flume
ENV HADOOP_HOME=/opt/hadoop
ENV JAVA_HOME=/usr/local/openjdk-17
ENV PATH=$PATH:$FLUME_HOME/bin:$HADOOP_HOME/bin

# 安装系统依赖（包括OpenSSL和Snappy）
RUN apt-get update && \
    apt-get install -y \
        wget \
        curl \
        # Snappy 相关依赖
        libsnappy1v5 \
        libsnappy-dev \
        # OpenSSL 依赖（解决libcrypto.so问题）
        libssl3 \
        libssl-dev \
        openssl \
        libcrypto++8 \
        # 其他必要依赖
        libc6-dev \
        zlib1g-dev \
        liblz4-1 \
        liblz4-dev \
        # 构建工具
        build-essential \
        pkg-config && \
    # 清理APT缓存
    apt-get clean && \
    rm -rf /var/lib/apt/lists/*

# 设置工作目录
WORKDIR /opt

# 下载并安装Hadoop
RUN wget -q http://ftp.tec-do.com/dev/flume/hadoop-${HADOOP_VERSION}.tar.gz && \
    tar -zxf hadoop-${HADOOP_VERSION}.tar.gz && \
    mv hadoop-${HADOOP_VERSION} ${HADOOP_HOME} && \
    rm hadoop-${HADOOP_VERSION}.tar.gz

# 下载并安装Flume
RUN mkdir -p ${FLUME_HOME} && \
    wget -q http://ftp.tec-do.com/dev/flume/apache-flume-${FLUME_VERSION}-bin.tar.gz && \
    tar zxf apache-flume-${FLUME_VERSION}-bin.tar.gz -C ${FLUME_HOME} --strip-components=1 && \
    rm apache-flume-${FLUME_VERSION}-bin.tar.gz

# 清理冲突的JAR文件
RUN rm -f ${FLUME_HOME}/lib/httpclient-4.2.1.jar \
          ${FLUME_HOME}/lib/httpcore-4.4.1.jar \
          ${FLUME_HOME}/lib/jackson-*.jar

# 下载JindoSDK（阿里云OSS支持）
RUN wget -q http://ftp.tec-do.com/dev/flume/jindosdk-${JINDOSDK_VERSION}-linux.tar.gz && \
    tar -zxf jindosdk-${JINDOSDK_VERSION}-linux.tar.gz && \
    mv jindosdk-${JINDOSDK_VERSION}-linux/lib/* ${FLUME_HOME}/lib/ && \
    rm -rf jindosdk-${JINDOSDK_VERSION}-linux.tar.gz jindosdk-${JINDOSDK_VERSION}-linux

# 下载必要的JAR依赖
RUN wget -q https://maven-local.tec-develop.cn/repository/nexus-aws/org/apache/hadoop/hadoop-hdfs/3.1.1/hadoop-hdfs-3.1.1.jar \
         -P ${FLUME_HOME}/lib/ && \
    wget -q http://maven-local.tec-develop.cn/repository/tecdo-releases/com/tec-do/flume-plugin/1.0.0/flume-plugin-1.0.0.jar \
         -P ${FLUME_HOME}/lib/ && \
    wget -q http://maven-local.tec-develop.cn/repository/tecdo-releases/com/tec-do/dsp-flume-plugin/1.0.0/dsp-flume-plugin-1.0.0.jar \
         -P ${FLUME_HOME}/lib/ && \
    wget -q http://ftp.tec-do.com/dev/flume/flume-interceptor-timestamp-delimiter-1.0.0.jar \
         -P ${FLUME_HOME}/lib/

# 配置环境变量和Java系统属性
RUN echo 'export HADOOP_HOME=/opt/hadoop' >> /etc/profile && \
    echo 'export HADOOP_CLASSPATH=/opt/hadoop/lib/*' >> /etc/profile && \
    echo 'export FLUME_HOME=/opt/flume' >> /etc/profile && \
    echo 'export PATH=$PATH:$FLUME_HOME/bin:$HADOOP_HOME/bin' >> /etc/profile

# 创建Flume配置目录
RUN mkdir -p ${FLUME_HOME}/conf ${FLUME_HOME}/logs

# 设置Hadoop和Flume的Java选项（解决原生库问题）
ENV HADOOP_OPTS="-Djava.library.path=${HADOOP_HOME}/lib/native -Dhadoop.security.crypto.codec.classes.aes.ctr.nopadding=org.apache.hadoop.crypto.JceAesCtrCryptoCodec"
ENV FLUME_JAVA_OPTS="-Djava.library.path=${HADOOP_HOME}/lib/native -Dhadoop.native.lib=false"

# 创建启动脚本
RUN echo '#!/bin/bash' > /opt/start-flume.sh && \
    echo 'source /etc/profile' >> /opt/start-flume.sh && \
    echo 'export HADOOP_OPTS="${HADOOP_OPTS}"' >> /opt/start-flume.sh && \
    echo 'export FLUME_JAVA_OPTS="${FLUME_JAVA_OPTS}"' >> /opt/start-flume.sh && \
    echo 'if [ -z "$1" ]; then' >> /opt/start-flume.sh && \
    echo '  echo "Usage: $0 <agent-name> [config-file]"' >> /opt/start-flume.sh && \
    echo '  echo "Example: $0 a1 /opt/flume/conf/flume.conf"' >> /opt/start-flume.sh && \
    echo '  exit 1' >> /opt/start-flume.sh && \
    echo 'fi' >> /opt/start-flume.sh && \
    echo 'AGENT_NAME=$1' >> /opt/start-flume.sh && \
    echo 'CONFIG_FILE=${2:-/opt/flume/conf/flume.conf}' >> /opt/start-flume.sh && \
    echo 'echo "Starting Flume Agent: $AGENT_NAME with config: $CONFIG_FILE"' >> /opt/start-flume.sh && \
    echo 'flume-ng agent --conf ${FLUME_HOME}/conf --conf-file $CONFIG_FILE --name $AGENT_NAME -Dflume.root.logger=INFO,console' >> /opt/start-flume.sh && \
    chmod +x /opt/start-flume.sh

# 创建示例Flume配置文件
RUN echo '# Example Flume Configuration' > ${FLUME_HOME}/conf/flume.conf && \
    echo '# Agent configuration' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.sources = r1' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.sinks = k1' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.channels = c1' >> ${FLUME_HOME}/conf/flume.conf && \
    echo '' >> ${FLUME_HOME}/conf/flume.conf && \
    echo '# Source configuration' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.sources.r1.type = spooldir' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.sources.r1.spoolDir = /opt/flume/spool' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.sources.r1.channels = c1' >> ${FLUME_HOME}/conf/flume.conf && \
    echo '' >> ${FLUME_HOME}/conf/flume.conf && \
    echo '# Channel configuration' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.channels.c1.type = memory' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.channels.c1.capacity = 10000' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.channels.c1.transactionCapacity = 1000' >> ${FLUME_HOME}/conf/flume.conf && \
    echo '' >> ${FLUME_HOME}/conf/flume.conf && \
    echo '# Sink configuration' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.sinks.k1.type = logger' >> ${FLUME_HOME}/conf/flume.conf && \
    echo 'a1.sinks.k1.channel = c1' >> ${FLUME_HOME}/conf/flume.conf

# 创建必要的目录
RUN mkdir -p ${FLUME_HOME}/spool ${FLUME_HOME}/logs

# 设置权限
RUN chmod +x ${FLUME_HOME}/bin/flume-ng && \
    chmod -R 755 ${FLUME_HOME} ${HADOOP_HOME}

# 验证安装
RUN echo "Flume version:" && \
    ${FLUME_HOME}/bin/flume-ng version

# 暴露端口（根据需要调整）
EXPOSE 44444 41414

# 设置工作目录
WORKDIR ${FLUME_HOME}

# 默认命令
CMD ["/opt/start-flume.sh"]
